package cn.com.dwr.modules.app.controller;

import cn.com.dwr.common.dto.OrderDto;
import cn.com.dwr.common.lang.Result;
import cn.com.dwr.entity.AppOrder;
import cn.com.dwr.modules.app.annotation.Login;
import cn.com.dwr.modules.app.annotation.NoRepeatSubmit;
import cn.com.dwr.service.AppCartItemService;
import cn.com.dwr.service.AppOrderService;
import cn.com.dwr.service.AppUserService;
import cn.hutool.core.map.MapUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/app/order")
public class OrderController extends BaseController{

    @Resource
    AppCartItemService appCartItemService;

    @Resource
    AppUserService appUserService;

    @Resource
    AppOrderService appOrderService;

    /**
     * 订单预览
     * @param orderDto
     * @return
     */
    @Login
    @PostMapping("/preview")
    public Result adpreviewd(@RequestBody OrderDto orderDto){
        return Result.success(appOrderService.preview(orderDto));
    }

    /**
     * 创建订单
     * @param orderDto
     * @return
     */
    @Login
    @NoRepeatSubmit
    @PostMapping("/create")
    public Result create(@RequestBody OrderDto orderDto){
        return Result.success(appOrderService.create(orderDto));
    }

    /**
     * 查询订单状态
     * @return
     */
    @Login
    @GetMapping("/getOrderCount")
    public Result getOrderCount(){
        return Result.success(appOrderService.getOrderCount());
    }

    /**
     * 订单列表
     * @param status
     * @return
     */
    @Login
    @GetMapping("/list")
    public Result list(Integer status){
        long userId = appUserService.getCurrentUserId();
        return Result.success(appOrderService.getPage(getPage(), userId, status));
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @Login
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Long id){
        return Result.success(appOrderService.detail(id));
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @Login
    @PostMapping("/cancel")
    public Result cancel(Long id){
        appOrderService.cancel(id);
        return Result.success();
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @Login
    @PostMapping("/delete")
    public Result delete(Long id){
        appOrderService.delete(id);
        return Result.success();
    }

    /**
     * 物流信息
     * @param id
     * @return
     */
    @Login
    @GetMapping("/deliveryInfo")
    public Result deliveryInfo(Long id) throws Exception {
        AppOrder appOrder = appOrderService.getOwnById(id);
        return Result.success(
                MapUtil.builder()
                        .put("deliveryInfo", appOrderService.getDeliveryInfo(id))
                        .put("deliveryCompany", appOrder.getDeliveryCompany())
                        .put("deliverySn", appOrder.getDeliverySn())
                        .build()
        );
    }

    /**
     * 确认收货
     * @param id
     * @return
     */
    @Login
    @PostMapping("/confirm")
    public Result confirm(Long id){
        appOrderService.confirm(id);
        return Result.success();
    }
}
